<?php

namespace App\Http\Requests\finance;

use App\Http\ValidateResult;
use Illuminate\Support\Facades\Validator;

trait SalaryRequest
{
    public static function add_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'name' => 'required|max:255|min:1',
            'type' => 'required|in:1,2',
        ], [
            'name.required' => '请填写名称',
            'type.required' => '请选择类型',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }
        return ValidateResult::correct();
    }

    public static function month_day_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'month' => 'required',
            'day' => 'required|integer|min:1|max:31',
        ], [
            'month.required' => '请选择月份',
            'day.required' => '请填写天数',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }
        return ValidateResult::correct();
    }

    public static function salary_dec_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'list' => 'required|array',
//            'salary_name_id' => 'required',
//            'amount' => 'required|numeric',
        ], [
            'list.required' => '数据为空',
//            'salary_name_id.required' => '请选择薪资名称',
//            'amount.required' => '请填写金额',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }
        return ValidateResult::correct();
    }

    public static function subsidy_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'amount' => 'required|numeric',
        ], [
            'amount.required' => '请填写金额',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }
        return ValidateResult::correct();
    }

    public static function edit_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'type' => 'required',
            'amount' => 'required|numeric',
        ], [
            'type.required' => '没有明细类型',
            'amount.required' => '请填写金额',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }
        return ValidateResult::correct();
    }

}
